//
//  NewsSheetViewController.m
//  优学荟管理系统
//
//  Created by edz on 2018/3/1.
//  Copyright © 2018年 星道三好. All rights reserved.
//
#import "NewsDetailVC.h"
#import "SawScrollTiitModel.h"
#import "NewsSheetViewController.h"
#import "SheetTabViewCell.h"
@interface NewsSheetViewController ()<UITableViewDelegate,UITableViewDataSource,CustomAlertDelegete>
{
    NSInteger _mark;//标记类型
    NSString *_selectDate;//标记选中的日期
}
@property(nonatomic,strong)NSArray *numInformationArr;
@property (nonatomic,strong) CustomDayDatePicker *datePicker;
@property(nonatomic,strong)NSArray *contentArr;
@property(nonatomic,strong)SawScrollTiitModel *scrolModel;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UIButton *starButton;
@property(nonatomic,strong)UIButton *endButton;
@end

@implementation NewsSheetViewController
-(void)btnindex:(int)index :(int)tag
{
    if (index == 2) {
        if (_mark == 1) {
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.datePicker.year,self.datePicker.month,self.datePicker.day];
            [self.starButton setTitle:[NSString stringWithFormat:@"%@",_selectDate] forState:(UIControlStateNormal)];
        }else if (_mark == 2){
            
            _selectDate = [NSString stringWithFormat:@"%d-%02d-%02d",self.datePicker.year,self.datePicker.month,self.datePicker.day];
            [self.endButton setTitle:[NSString stringWithFormat:@"%@",_selectDate] forState:(UIControlStateNormal)];
            
            [self sureButtonAction];
        }
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"咨询列表";
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_async(group, queue, ^{
        
            [self.scrolModel dataWithSheet:^(NSArray *dataArr) {
                
                self.numInformationArr = dataArr;
              
               
                [self.scrolModel cellWithSheet:[dataArr[0][@"id"] integerValue] WithDataArr:^(NSArray *dataArr) {
                    self.contentArr = dataArr;
                    
                    dispatch_semaphore_signal(semaphore);
                }];
          
                
            }];
        
    });
 
    dispatch_group_notify(group, queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
      
        
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self setUI];
            
        });
     
    });
        
  
}
-(SawScrollTiitModel *)scrolModel
{
    if (!_scrolModel) {
        _scrolModel = [[SawScrollTiitModel alloc]init];
    }
    return _scrolModel;
}
-(NSArray *)numInformationArr
{
    if (!_numInformationArr) {
        _numInformationArr = [[NSArray alloc]init];
    }
    
    return _numInformationArr;
}
-(NSArray *)contentArr
{
    if (!_contentArr) {
        _contentArr = [NSArray array];
    }
    
    return _contentArr;
}
-(void)setUI
{
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
    [self.view addSubview:scrollView];
    scrollView.backgroundColor=[UIColor whiteColor];
    NSLog(@"...%lu",(unsigned long)self.numInformationArr.count);
    scrollView.contentSize = CGSizeMake(self.numInformationArr.count*100, 50);
    for (int i=0; i<self.numInformationArr.count; i++) {
        UIButton *NumBtn = [[UIButton alloc]initWithFrame:CGRectMake(15+i*(30+70), 0, 70, 50)];
        [scrollView addSubview:NumBtn];
        NumBtn.tag=5000+i;
        [NumBtn addTarget:self action:@selector(seleNum:) forControlEvents:UIControlEventTouchUpInside];
        [NumBtn setTitle:self.numInformationArr[i][@"categoryName"] forState:0];
        [NumBtn setTitleColor:[UIColor blackColor] forState:0];
    }
    
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(scrollView.frame), kScreenWidth, kScreenHeight-50-64)];
    _tableView.rowHeight=80;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[SheetTabViewCell class] forCellReuseIdentifier:@"Cell"];
    [_tableView setTableHeaderView:[self getHeaderView]];
    [self.view addSubview:_tableView];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    
}
-(void)seleNum:(UIButton *)sender
{
    NSInteger num = [self.numInformationArr[sender.tag-5000][@"id"] integerValue];
    [self.scrolModel cellWithSheet:num WithDataArr:^(NSArray *dataArr) {
        self.contentArr = dataArr;
        [_tableView reloadData];
    }];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.contentArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SheetTabViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSDictionary *dic = self.contentArr[indexPath.row];
    cell.titleLab.text=dic[@"title"];
    cell.dateLab.text=dic[@"createTime"];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.contentArr[indexPath.row];
    
    NewsDetailVC *vc = [[NewsDetailVC alloc]init];
   // vc.url=dic[@"content"];  
    [self.navigationController pushViewController:vc
                                         animated:YES];
}
-(UIView *)getHeaderView
{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 110)];
    headView.backgroundColor=RGBColor(245, 245, 245);
    
    UILabel *lab = [[UILabel alloc]init];
    [headView addSubview:lab];
    lab.text=@"选择时间";
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(26);
        make.top.offset(20);
    }];
    
    _starButton = [[UIButton alloc]init];
    [headView addSubview:_starButton];
    [self.starButton setTitle:[NSString getCurrentDateYearMD] forState:(UIControlStateNormal)];
    _starButton.backgroundColor=[UIColor whiteColor];
    [_starButton setFont:[UIFont systemFontOfSize:13]];
    [_starButton setTitleColor:RGBColor(102, 102, 102) forState:0];
    [_starButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(20);
        make.top.equalTo(lab.mas_bottom).offset(14);
        make.width.offset(kARWidthTranslator(60));
        make.height.offset(30);
    }];
    
    _endButton = [[UIButton alloc]init];
    [headView addSubview:_endButton];
     [self.endButton setTitle:[NSString currentDateTow] forState:(UIControlStateNormal)];
    [_endButton setFont:[UIFont systemFontOfSize:13]];
    _endButton.backgroundColor=[UIColor whiteColor];
    [_endButton setTitleColor:RGBColor(102, 102, 102) forState:0];
    [_endButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_starButton.mas_right).offset(20);
        make.top.equalTo(lab.mas_bottom).offset(14);
        make.width.offset(kARWidthTranslator(60));
        make.height.offset(30);
    }];
    self.starButton.tag = 201;
    self.endButton.tag = 202;
    [self.starButton addTarget:self action:@selector(allButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [self.endButton addTarget:self action:@selector(allButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    UIButton *sureBtn = [[UIButton alloc]init];
    [headView addSubview:sureBtn];
    [sureBtn setTitle:@"确定" forState:0];
    [sureBtn setBackgroundImage:[UIImage imageNamed:@"btn_ensure_default"] forState:0];
    [sureBtn addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.endButton.mas_right).offset(20);
        make.top.equalTo(lab.mas_bottom).offset(14);
        make.width.offset(60);
        make.height.offset(30);
    }];
    
    return headView;
}
- (void)allButtonAction:(UIButton *)sender{
    
    NSInteger tag = sender.tag;
    
    
    if (201 == tag) {
        JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 60, 200)];
        alert.center = self.view.center;
        alert.delegate = self;
        [alert initwithtitle:@"开始时间" andcommitbtn:@"确定" andStr:@"0"];
        [alert addSubview:self.datePicker];
        [alert show];
        
        
        _mark = 1;
        
    }else{
        
        JXAlertview *alert = [[JXAlertview alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 60, 200)];
        alert.center = self.view.center;
        alert.delegate = self;
        [alert initwithtitle:@"结束时间" andcommitbtn:@"确定" andStr:@"0"];
        [alert addSubview:self.datePicker];
        [alert show];
        
        
        _mark = 2;
        
    }
    
    
    
    
}
- (CustomDayDatePicker *)datePicker {
    
    if (_datePicker == nil) {
        self.datePicker = [[CustomDayDatePicker alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth - 60, 140)];
        
    }
    return _datePicker;
}
#pragma mark --- 确定方法的实现
- (void)sureButtonAction{
    
    NSString *start = [self.starButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    NSString *endSelect = [self.endButton.currentTitle stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    
    NSComparisonResult result = [start compare:endSelect];
    
    if (result != NSOrderedAscending) {
        [SVProgressHUD showErrorWithStatus:@"结束时间必须大于开始时间!"];
    }else{
        
        
        [self setSetTowSureStart:start endSelect:endSelect];
        
        
        
    }
    
    
    
}
- (void)setSetTowSureStart:(NSString *)startStr endSelect:(NSString *)endSelect{
    
    
    [self.starButton setTitle:startStr forState:(UIControlStateNormal)];
    [self.endButton setTitle:endSelect forState:(UIControlStateNormal)];
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
